!function () {
    layui.config({base: '/layui/lay/modules/'}).use(['upload','form'], function () {
        //定义
        var $ = layui.jquery,
            form = layui.form,
            upload = layui.upload;

        //启动富文本编辑
        var ueditor ;
        //初始化渲染列表
        var typeVal = $('input[name=typeValue]').val();
        $.ajax({
            async: false,
            url: '/help/findHelpList',
            success: function (result) {
                if (result && result.code == 200) {
                    //存储字典项
                    if( result.data && result.data.length>0  ){
                        var html ='<option value="">全部</option>';
                        $.each(result.data,function(key,obj){
                            //存储当前字典项
                            if( obj.helpId == typeVal ){
                                html += '<option value="'+obj.helpId+'" selected>'+obj.helpName+'</option>';
                            }else{
                                html += '<option value="'+obj.helpId+'">'+obj.helpName+'</option>';
                            }
                        });
                        $('#type').empty().append(html);
                    }
                }
            }
        });
        form.render();

        //清空富文本编辑框
        ueditor = UE.getEditor('ueditorText');
        $('#edui1').css({"z-index":880});
        //玄学加载
        setTimeout(function () {
            ueditor.setContent($("#assistInfoText").text());
        }, 666);
        $('.edui-editor-bottomContainer').hide();
        //普通图片上传
        var uploadFileBtn = upload.render({
            elem: '#uploadBtn'
            ,url: '/upload/uploadFiles'
            ,field:"layuiFiles"
            ,accept: 'file' //普通文件
            ,auto: false    //不自动上传
            ,exts: 'jpg|png|jpeg' //只允许上传压缩文件
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    var $uploadListDiv = $('#uploadListDiv');
                    var htmlDiv ='';
                    if(file.type.indexOf("image") !=-1){
                        if( (file.size/1014).toFixed(1) > 10240 ){
                            return layer.msg("图片不能超过10M");
                            return false;
                        }
                        uploadFileBtn.config.data ={"dir":"image"};
                        htmlDiv +='<img id="fileDiv" style="height: 137px;width: 137px;"/>';
                    }
                    $uploadListDiv.empty().append(htmlDiv);
                    //执行上传
                    obj.upload(index, file);
                });
            }
            ,done: function(res){
                if(res.code==1){
                    layer.alert(res.message,5);
                }
                if(res.error>0){
                    return layer.msg(res.message);
                }
                if(res.error==0){
                    var $uploadListDiv = $('#uploadListDiv');
                    $("#url").val(res.url);
                    $uploadListDiv.find("#fileDiv").attr("src",res.url);
                    layer.msg("上传成功");
                }
            }
        });

        //自定义验证规则
        form.verify({
            titleLen: [
                /^[\d\D]{1,128}$/
                ,'内容标题最多只能输入128个字'
            ],
            textLen:[
                /^[\d\D]{0,200}$/
                ,'最多输入200个字符'
            ],
        });

        $('#backRecord').click(function(){
            window.location.href="/assistInfo/list";
        });

        //监听提交事件
        form.on('submit(editRecord)',function(data){

            //获取富文本编辑内容
            var assistInfoText = ueditor.getContent();
            data.field['assistText']=assistInfoText;

            var postUrl = !data.field['assistId'] ? '/assistInfo/addAssistInfo' : '/assistInfo/editAssistInfo';
            $.ajax({
                async: false, type: 'post',
                traditional: true,
                data: JSON.stringify(data.field),
                dataType : 'json',
                contentType:'application/json;charset=UTF-8',//contentType很重要
                processData:false,
                url: postUrl,
                success: function (data) {
                    if (data && data.code == 200) {
                        layer.closeAll();
                        layer.alert('操作成功！', {icon: 1, title: '提示',yes:function(){
                                window.location.href="/assistInfo/list";
                            }});
                    } else
                        layer.alert(data.msg || '操作失败了！', {icon: 5, title: '提示'});
                },
                error: function () {
                    layer.closeAll();
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });
            return false;
        });

    });
}();